package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Finance;
import com.ruoyi.system.service.IFinanceService;
import com.ruoyi.system.service.IWorkerService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.EquipmentPurchase;
import com.ruoyi.system.service.IEquipmentPurchaseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备购买Controller
 *
 * @author ruoyi
 * @date 2024-05-08
 */
@RestController
@RequestMapping("/system/purchase")
public class EquipmentPurchaseController extends BaseController
{
    @Autowired
    private IEquipmentPurchaseService equipmentPurchaseService;

    @Autowired
    private IWorkerService workerService;

    @Autowired
    private IFinanceService financeService;
    /**
     * 查询设备购买列表
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:list')")
    @GetMapping("/list")
    public TableDataInfo list(EquipmentPurchase equipmentPurchase)
    {
        startPage();
        List<EquipmentPurchase> list = equipmentPurchaseService.selectEquipmentPurchaseList(equipmentPurchase);
        return getDataTable(list);
    }

    /**
     * 导出设备购买列表
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:export')")
    @Log(title = "设备购买", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EquipmentPurchase equipmentPurchase)
    {
        List<EquipmentPurchase> list = equipmentPurchaseService.selectEquipmentPurchaseList(equipmentPurchase);
        ExcelUtil<EquipmentPurchase> util = new ExcelUtil<EquipmentPurchase>(EquipmentPurchase.class);
        util.exportExcel(response, list, "设备购买数据");
    }

    /**
     * 获取设备购买详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:query')")
    @GetMapping(value = "/{purchaseId}")
    public AjaxResult getInfo(@PathVariable("purchaseId") Long purchaseId)

    {
        return success(equipmentPurchaseService.selectEquipmentPurchaseByPurchaseId(purchaseId));
    }

    /**
     * 新增设备购买
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:add')")
    @Log(title = "设备购买", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EquipmentPurchase equipmentPurchase)
    {
        Finance finance = new Finance();
        finance.setFType("设备购买");
        finance.setFAmount(String.valueOf(equipmentPurchase.getPurchasePrice()));
        finance.setFDate(equipmentPurchase.getPurchaseDate());
        finance.setFStuff(equipmentPurchase.getPayWorker());
        finance.setFRe("无");
        return toAjax((equipmentPurchaseService.insertEquipmentPurchase(equipmentPurchase) & financeService.insertFinance(finance)));
    }

    /**
     * 修改设备购买
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:edit')")
    @Log(title = "设备购买", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EquipmentPurchase equipmentPurchase)
    {
        return toAjax(equipmentPurchaseService.updateEquipmentPurchase(equipmentPurchase));
    }

    /**
     * 删除设备购买
     */
    @PreAuthorize("@ss.hasPermi('system:purchase:remove')")
    @Log(title = "设备购买", businessType = BusinessType.DELETE)
	@DeleteMapping("/{purchaseIds}")
    public AjaxResult remove(@PathVariable Long[] purchaseIds)
    {
        return toAjax(equipmentPurchaseService.deleteEquipmentPurchaseByPurchaseIds(purchaseIds));
    }
}
